package com.kelvem.saas.workbanch.core.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kelvem.saas.workbanch.fsm.entity.Entity;

import java.util.Collection;
import java.util.List;

public class BaseCriteria<T extends Entity> {

    public BaseCriteria() {
        this.wrapper = new QueryWrapper<T>();
    }
    private QueryWrapper<T> wrapper;

    public QueryWrapper<T> wrapper() {
        return this.wrapper;
    }

    public void orderByAsc(String column, String... columns) {
        this.wrapper.orderByAsc(camelToUnderscore(column), camelToUnderscore(columns));
    }

    public void orderByDesc(String column, String... columns) {
        this.wrapper.orderByDesc(camelToUnderscore(column), camelToUnderscore(columns));
    }

    public void andEqualTo(String column, Object val) {
        this.wrapper.eq(this.isNotEmpty(val), camelToUnderscore(column), val);
    }

    public void andGreaterThanOrEqualTo(String column, Object val) {
        this.wrapper.ge(this.isNotEmpty(val), camelToUnderscore(column), val);
    }

    public void andLessThanOrEqualTo(String column, Object val) {
        this.wrapper.le(this.isNotEmpty(val), camelToUnderscore(column), val);
    }

    public void andIn(String column, List<?> list) {
        this.wrapper.in(this.isNotEmpty(list), camelToUnderscore(column), list);
    }



    /**
     * 判断对象是否为null,字段串"",null返回false,其它null返回false
     *
     * @param obj 被检查对象
     * @return 空:true, 非空:false
     */
    private boolean isNotEmpty(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof String) {
            if (((String) obj).trim().isEmpty()) {
                return false;
            } else {
                return true;
            }
        }
        if (obj instanceof Collection) {
            Collection<?> col = (Collection<?>) obj;
            return !col.isEmpty();
        }
        return true;
    }
    /**
     * 将驼峰式命名转换为下划线分隔的命名。
     *
     * @param camelCaseName 驼峰式命名的字符串
     * @return 下划线分隔的命名
     */
    private String camelToUnderscore(String camelCaseName) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < camelCaseName.length(); i++) {
            char ch = camelCaseName.charAt(i);
            if (Character.isUpperCase(ch)) {
                // 如果不是第一个字符，那么在大写字母前添加下划线
                if (i > 0) {
                    result.append('_');
                }
                result.append(Character.toLowerCase(ch));
            } else {
                result.append(ch);
            }
        }
        return result.toString();
    }

    private String[] camelToUnderscore(String[] camelCaseNames) {
        if (camelCaseNames == null) {
            return null;
        }

        String[] result = new String[camelCaseNames.length];
        for (int i = 0; i < camelCaseNames.length; i++) {
            String camelCaseName = camelCaseNames[i];
            result[i] = camelToUnderscore(camelCaseName);
        }
        return result;
    }
}
